<?php

/**
 * Other(业务管理---其它业务管理 后台左侧栏目管理)
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class OtherController extends AdminbaseController
{


    function _initialize()
    {
        parent::_initialize();
//        $this->sys_menu_model = D("Common/SysMenu");
//        $this->sys_menu_admin_model = D("Common/SysMenuAdmin");
    }

    /*
     * 显示侧边栏 列表
     * */
    public function defaultIndex(){
        $_SESSION['admin_menusset_index'] = "Other/defaultIndex";

        $currentPage = I('param.p', 0);
        if ($currentPage <= 0) {
            $currentPage = 1;
        }

        $pageStart = ($currentPage - 1) * 10;

        $menu_res = array();

        if($_SESSION['ADMIN_ID'] == 1){
            $status = 1; //超级管理员
            //超级管理员 显示所有
            $menu = M("vradmin1.sys_menu",null);
            $menu_con = $menu
                ->join("vradmin1.sys_menu_admin on sys_menu.menu_id = sys_menu_admin.menu_id",'left')
                ->join("vradmin1.admin_user  on vradmin1.sys_menu_admin.admin_id = vradmin1.admin_user.admin_id",'left')
                ->where("sys_menu.menu_id in(166,286,306)")
                ->Group("sys_menu.menu_id")
                ->select();
            $menu_res['count'] = count($menu_con);

            $menu_res['menus'] = $menu
                ->join("vradmin1.sys_menu_admin on sys_menu.menu_id = sys_menu_admin.menu_id",'left')
                ->join("vradmin1.admin_user  on vradmin1.sys_menu_admin.admin_id = vradmin1.admin_user.admin_id",'left')
                ->where("sys_menu.menu_id in(166,286,306)")
                ->field("*,group_concat(vradmin1.sys_menu_admin.admin_id ) admin_ids,
group_concat(vradmin1.admin_user.username ) usernames,sys_menu.menu_id as menu_id")
                ->Group("sys_menu.menu_id")
                ->limit($pageStart, 10)
                ->select();
            $this->assign("data",$menu_res['menus']);

        }else{
            $status = 2; //普通管理员
            $menu = M("vradmin1.sys_menu_admin",null);
            $menu_con = $menu
                ->join("vradmin1.sys_menu on vradmin1.sys_menu_admin.menu_id = sys_menu.menu_id",'left')
                ->where("sys_menu_admin.admin_id = ".$_SESSION['ADMIN_ID']." and sys_menu_admin.menu_id not in(294,295,296,305,317) and sys_menu.parentid = 0")
//                ->where("sys_menu_admin.admin_id = 18")
                ->select();
            $menu_res['count'] = count($menu_con);

            $menu_res['menus'] = $menu
                ->join("vradmin1.sys_menu on vradmin1.sys_menu_admin.menu_id = sys_menu.menu_id",'left')
                ->where("sys_menu_admin.admin_id = ".$_SESSION['ADMIN_ID']." and sys_menu_admin.menu_id not in(294,295,296,305,317) and sys_menu.parentid = 0")
                ->limit($pageStart, 10)
//                ->where("sys_menu_admin.admin_id = 18")
                ->select();
            $this->assign("data",$menu_res['menus']);
        }
//print_r($menu_res);die;
        //分页
        $page = $this->page($menu_res['count'], 10);
        $this->assign("page", $page->show('Admin'));
        
        $this->assign("status",$status);
        $this->display('defaultindex');
    }

    //显示 修改窗
    public function editAdmin(){
        $menu_id   = I("menu_id");
        $admin_ids = I("admin_ids");
        $usernames = I("usernames");


        if(empty($usernames)){
            $arr_usernames = '';
        }else{
            $arr_usernames = explode(',',$usernames);
        }
        if(empty($admin_ids)){
            $arr_admin_ids = '';
        }else{
            $arr_admin_ids = explode(',',$admin_ids);
        }

        //全部 管理员
        $admins = M("vradmin1.admin_user",null);
        $admin_infos = $admins
            ->where("type = 2 and status = 1")
            ->select();

        $this->assign("arr_admin_ids",$arr_admin_ids);
        $this->assign("arr_usernames",$arr_usernames);
        $this->assign("admin_infos",$admin_infos);
        $this->assign("admin_ids",$admin_ids);
        $this->assign("menu_id",$menu_id);
        $this->assign("usernames",$usernames);
        $this->display('editadmin');
    }

    //编辑 管理员
    public function editAdminPost(){
        $result = '0';
        $menu_id = I("param.menu_id", "");
        $admin_ids = rtrim(I("param.admin_hidden_ids", ""),',');

        if (empty($menu_id)) {
            echo $result;exit;
        }
        //查看 业务对应的子级 ID
        $menu_model = M("vradmin1.sys_menu",null);
        $menu_get = $menu_model->where("parentid = ".$menu_id)->field("menu_id")->select();
        $menu_sons = array_column($menu_get,"menu_id" );
        array_push($menu_sons,$menu_id );

        $menu_sons_str = implode(',',$menu_sons );

        //设置管理员
        if($admin_ids){
            $admin_arr = explode(',', $admin_ids);

            foreach($admin_arr as $admin_key=>$admin_value){
                foreach ($menu_sons as $v){
                    $dataList[] = array('menu_id'=>$v,'admin_id'=>$admin_value);
                }
            }

            $admin_model = M("vradmin1.sys_menu_admin",null);
//            $menu_sel = $admin_model->where("menu_id = $menu_id")->select();

//            if($menu_sel){
                $admin_model->where('menu_id in ('.$menu_sons_str.")")->delete();

//            }
            if($admin_model->addAll($dataList)) echo 1;
        }else{
            $admin_model = M("vradmin1.sys_menu_admin",null);
//            $menu_sel = $admin_model->where("menu_id = $menu_id")->select();

//            if($menu_sel) {
                $admin_model->where('menu_id in (' . $menu_sons_str.")")->delete();
//            }
            echo 1;
        }

    }


}